/**
 * @class   wosh::FormBusViewer
 * @brief   Graphic tool showing messages on a Bus
 *          
 *          
 *
 ****************************************************************************
 * @version $Id: FormBusViewer.h 2663 2010-06-16 15:46:24Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_FormBusViewer_H__
 #define __WOSH_Gui_FormBusViewer_H__

 #include <core/Bus.h>
 #include <core/Containers.h>
 #include <core/BusConnector.h>
 #include <core/Message.h>

 #include <QtGui/QWidget>
 #include <QtGui/QMenu>
 #include <QAbstractTableModel>


using namespace wosh;
namespace Ui {
 class FormBusViewer;
};
namespace wosh {
 namespace gui {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

class MessageTableModel : public QAbstractTableModel
 {
	Q_OBJECT

	public:
		MessageTableModel( ListT<Message*>& messageCache, QObject *parent = 0)
			: QAbstractTableModel(parent), messages(messageCache) { }

		virtual ~MessageTableModel() { }

		using QAbstractTableModel::reset;

	public:

		virtual int rowCount(const QModelIndex &parent = QModelIndex()) const {
			(void)parent;
			return this->messages.count();
		 }

		virtual int columnCount(const QModelIndex &parent = QModelIndex()) const {
			(void)parent; return 4;
		 }

		virtual QVariant data(const QModelIndex &index, int role) const {
			if (  !index.isValid() ) return QVariant();
			if ( role == Qt::DisplayRole || role == Qt::EditRole ) {
				MutexLockerRead mL(this->messages.getMutex());
				Message* message = this->messages.itemAt(index.row());
				if ( message == NULL ) return QVariant();
				switch(index.column()) {
					case 0: return QVariant( message->getID() );
					case 1: return QVariant( message->getSource().toString().c_str() );
					case 2: return QVariant( message->getDestination().toString().c_str() );
					case 3: return QVariant( message->getContentClassName().c_str() );
					default: return QVariant();
				 }
			 }
			else if ( role == Qt::BackgroundColorRole ) { }
			else if ( role == Qt::ToolTipRole ) { }
			return QVariant();
		 }

		virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const {
			if (role != Qt::DisplayRole) return QVariant();
			 if (orientation == Qt::Horizontal ) {
				switch(section) {
					case 0: return QVariant("ID");
					case 1: return QVariant("Source");
					case 2: return QVariant("Destination");
					case 3: return QVariant("Content-ClassName");
					default: return QVariant();
				 }
			 }
			return QVariant();
		 }

	private:
		 ListT<Message*>& messages;

}; // class def

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

class FormBusViewer :	public QWidget,
						public virtual IBusListener
 {
	Q_OBJECT

	WOSH_CLASSNAME(wosh::gui::FormBusViewer)
	WOSH_CLASS_ABSTRACT(false)


	public:
		FormBusViewer(QWidget *parent = 0);
		virtual ~FormBusViewer();

	public slots:
		virtual void refreshAll();

		void connectBus( const QString& busname );
		void disconnectBus( const QString& busname );

	public:
		WRESULT connectBus( Bus* bus );
		WRESULT disconnectBus( Bus* bus );

	public:
		void busMessage( const Message& message, const Bus* source );

	protected slots:
		void tableView_doubleClicked( const QModelIndex& );
		void menuColumns_triggered( QAction* );

	protected:
		void changeEvent(QEvent *e);
		void changeModel(MessageTableModel* model);


	protected:
//		BusConnector BusGui;
		int messageCacheMax;
		ListT<Message*> messageCache;

		MessageTableModel* wTableModel;

		QMenu* menuColumns;

	private:
		Ui::FormBusViewer *m_ui;

//_________________________________________________________________________

	protected slots:

		void event_filter_sourcebus( int row );
		void event_filter_destinationbus( int row );
		void event_filter_source( const URI& uri );
		void event_filter_destination( const URI& uri );
		void event_message_max( int value );

	protected:
		std::string filterSourceBus;
		std::string filterDestinationBus;
		bool filterSourceURI;
		bool filterDestinationURI;



}; // class def

 }; // namespace gui
}; // namespace wosh


#endif // __WOSH_Gui_FormBusViewer_H__
